package com.gyx.lottery.controller;

import com.gyx.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.gyx.lottery.common.exception.ControllerException;
import com.gyx.lottery.common.pojo.CommonResult;
import com.gyx.lottery.common.utils.JacksonUtil;
import com.gyx.lottery.controller.param.CreateAcivityParam;
import com.gyx.lottery.controller.param.PageParam;
import com.gyx.lottery.controller.result.ActivitiDetailResult;
import com.gyx.lottery.controller.result.CreateActivityResult;
import com.gyx.lottery.controller.result.FindActivityListResult;
import com.gyx.lottery.service.ActivityService;
import com.gyx.lottery.service.dto.ActivityCreateDTO;
import com.gyx.lottery.service.dto.ActivityDTO;
import com.gyx.lottery.service.dto.ActivityDetailDTO;
import com.gyx.lottery.service.dto.PageListDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

@RestController
public class ActivityController {
    private static final Logger logger= LoggerFactory.getLogger(ActivityController.class);
    @Autowired
    private ActivityService activityService;
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult>createActivity(@Validated @RequestBody CreateAcivityParam param){
        logger.info("createActivity 中 :createActivity为:{}",
                JacksonUtil.writeVauleAsString(param));
        ActivityCreateDTO activityCreateDTO=activityService.createActivity(param);
        return CommonResult.success(convertToCreateActivityResult(activityCreateDTO));
    }

    private CreateActivityResult convertToCreateActivityResult(ActivityCreateDTO activityCreateDTO) {
        CreateActivityResult result=new CreateActivityResult();
        result.setActivityId(activityCreateDTO.getActivityId());
        return result;
    }
    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult>findActivityList(PageParam param){
        logger.info("findActivityList :PageParam:{} ",
                JacksonUtil.writeVauleAsString(param));
        PageListDTO<ActivityDTO> activityDTOPageListDTO=activityService.findList(param);
        return CommonResult.success(convertToFindActivityListResult
                (activityDTOPageListDTO));

    }
    @RequestMapping("/activity-detail/find")
    public CommonResult<ActivitiDetailResult>getActivityDetail(Long activityId){
        logger.info("getActivityDetail :activityId:{}",activityId);
        ActivityDetailDTO activityDetailDTO=activityService.searchActivityDetail(activityId);
        return CommonResult.success(
                covertToActivitiDetailResult(activityDetailDTO));
    }

    private ActivitiDetailResult covertToActivitiDetailResult(ActivityDetailDTO auDTO) {
        if(auDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITY_DETAIL_ERROR);
        }
        ActivitiDetailResult result=new ActivitiDetailResult();
        result.setActivityId(auDTO.getActivityId());
        result.setActivityName(auDTO.getActivityName());
        result.setDescription(auDTO.getDesc());
        result.setValid(auDTO.valid());
        result.setPrizes(auDTO.getPrizeDTOList().stream()
        .map(prizeDTO->{
            ActivitiDetailResult.Prize prize=new ActivitiDetailResult.Prize();
            prize.setPrizeId(prizeDTO.getPrizeId());
            prize.setName(prizeDTO.getName());
            prize.setDescription(prizeDTO.getDescription());
            prize.setPrice(prizeDTO.getPrice());
            prize.setImageUrl(prizeDTO.getImageUrl());
            prize.setPrizeAmount(prizeDTO.getPrizeAmount());
            prize.setPrizeTierName(prizeDTO.getPrizeTiers().getMessage());
            prize.setValid(prizeDTO.valid());
                return prize; }
        ).collect(Collectors.toList()));
        result.setUsers(auDTO.getUserDTOList().stream()
        .map(
                userDTO->{
                    ActivitiDetailResult.User user=new ActivitiDetailResult.User();
                    user.setUserId(userDTO.getUserId());
                    user.setUserName(userDTO.getUserName());
                    user.setValid(userDTO.valid());
                    return  user; }
        )
        .collect(Collectors.toList()));

        return result;
    }

    private FindActivityListResult convertToFindActivityListResult(PageListDTO<ActivityDTO> activityDTOPageListDTO) {
        if(activityDTOPageListDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITY_LIST_ERROR);
        }
        FindActivityListResult result=new FindActivityListResult();
        result.setTotal(activityDTOPageListDTO.getTotal());
        result.setRecords(activityDTOPageListDTO.getRecords()
                .stream()
                .map(activityDTO -> {
                    FindActivityListResult.ActivityInfo activityInfo=new FindActivityListResult.ActivityInfo();
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setDescription(activityDTO.getDescription());
                    activityInfo.setValid(activityDTO.valid());
                    activityInfo.setActivityId(activityDTO.getActivityId());
                    return  activityInfo;
                }).collect(Collectors.toList()));
        return  result;
    }

}
